package com.sgb.knowledgeshare.view.activity;


import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.ActivityVideoCoverBinding;
import com.sgb.knowledgeshare.view.adapter.VideoCoverAdapter;
import com.sgb.knowledgeshare.weight.VideoCoverImageView;
import com.swgk.core.base.AppActivity;
import com.swgk.core.util.ThreadUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by fq on 2020/9/23 0023
 * Description：视频封面界面
 */
public class SelectVideoCoverActivity extends AppActivity {

    private ActivityVideoCoverBinding binding;
    private String videoPath;
    private List<Bitmap> list;
    private int VideoTime = 60 * 1000;
    private int timesizes;

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_video_cover);
        View decor = getWindow().getDecorView();
        decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding.videoCoverBack.setOnClickListener(this);
        binding.videoCoverSave.setOnClickListener(this);
        videoPath = getIntent().getStringExtra("videoPath");
        timesizes = getIntent().getIntExtra("time_size", 0);
        //视频图片处理
        if (!TextUtils.isEmpty(videoPath)) {
            VideoViewImageView(videoPath);
        }

        binding.videoCover.setOnScrollBorderListener(new VideoCoverImageView.OnScrollBorderListener() {
            @Override
            public void OnScrollBorder(float start, float end) {
                // 游标变更
                int startTIme = (int) start;
                float result = startTIme / (float)  binding.videoCover.getWidth();
            /*    mCursor = (int) (result * mDuration);
                startDurationTime = String.valueOf(start);*/
            }

            @Override
            public void onScrollStateChange() {

            }
        });

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();//保存
        if (id == R.id.video_cover_back) {//取消
            finish();
        } else if (id == R.id.video_cover_save) {
        }
    }

    //视频信息图片处理
    private void VideoViewImageView(String videoPath) {
        list = new ArrayList();
        ThreadUtils.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
                metadataRetriever.setDataSource(videoPath, new HashMap<>());
                String duration = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
                Log.d("帧数图", "duration = " + duration);
                int durationMs = Integer.parseInt(duration);
                //每秒取一次
                for (int i = 0; i < durationMs; i += 1000) {
                    long start = System.nanoTime();
                    Log.d("帧数图", "getFrameAtTime time = " + i);
                    //这里传入的是ms
                    Bitmap frameAtIndex = metadataRetriever.getFrameAtTime(i *1000);
                    Bitmap frame = Bitmap.createScaledBitmap(frameAtIndex, frameAtIndex.getWidth() / 8, frameAtIndex.getHeight() / 8, false);
                    list.add(frame);
                    Log.d("帧数图", "run: " + frame.toString());
                    Log.d("哈哈哈哈啊", "onCreate: " + list.size());
                    frameAtIndex.recycle();
                    long end = System.nanoTime();
                    long cost = end - start;
                    Log.d("帧数图", "cost time in millis = " + (cost * 1f / 1000000));
                    Log.d("哈哈哈哈啊1", "onCreate: " + list.size());
                }
                metadataRetriever.release();
            }
        });
        LinearLayoutManager layout = new LinearLayoutManager(this);
        layout.setOrientation(LinearLayoutManager.HORIZONTAL);//设置为横向排列
        binding.recyclerview.setLayoutManager(layout);
        VideoCoverAdapter adapter = new VideoCoverAdapter(this,list);
        binding.recyclerview.setAdapter(adapter);
    }
}
